var parentTr;

document.onkeydown = hotkeys;
function hotkeys(evt){
	evt = evt || window.event;
	var a = evt.keyCode;
	if((a==65)&&(evt.ctrlKey)&&(evt.shiftKey)){ // ctl + shift + a
		addMarcField();
	}else if((a==73)&&(evt.ctrlKey)&&(evt.shiftKey)){ // ctl + shift + i
		insMarcField();
	}else if((a==68)&&(evt.ctrlKey)&&(evt.altKey)){ // ctl + alt + d
		delMarcField();
	}else if((a==83)&&(evt.ctrlKey)&&(evt.shiftKey)){ // ctl + shift + s
		addMarcSubField();
	}
}

function focusID(inputObj){
	parentTr=inputObj.parentNode;
}

function addMarcField(){
	jConfirm(addConfirm,"",function(r){
		if(r)
			$("#marcTable").append(fixedInputHTML());
		else $("#marcTable").append(fieldInputHTML());
	},{okButton:noIndConfirm,cancelButton:hasIndConfirm}
	);
}

function insMarcField(){
	if(typeof(parentTr) == "undefined" || parentTr==null){
		if(confirm(insWarnConfirm)){
			addMarcField();
		}
	}else{
		jConfirm(addConfirm,"",function(r){
			if(r)
				$(parentTr.parentNode).after(fixedInputHTML());
			else $(parentTr.parentNode).after(fieldInputHTML());
		},{okButton:noIndConfirm,cancelButton:hasIndConfirm}
		);
	}
}

function delMarcField(){
	if(typeof(parentTr) == "undefined" || parentTr==null){
		alert(delwarn);
	}else{
		if(confirm(delConfirm)){
			$(parentTr.parentNode).remove(); 
			parentTr=null;
		}
	}
}

function addMarcSubField(){
	if(typeof(parentTr) == "undefined" || parentTr==null){
		alert(warnConfirm);
	}else{
		$(parentTr).append(subFieldInputHTML()); 
	}
}

function fixedInputHTML(){
	return '<tr><td><input name="fieldtag" size="3" maxlength="3" onfocus="focusID(this)" type="text"></td><td>&nbsp;</td><td>&nbsp;</td><td><input name="fixedtxt" size="30" onfocus="focusID(this)" type="text"></td></tr>';
}

function fieldInputHTML(){
	return '<tr><td valign="top"><input name="fieldtag" size="3" maxlength="3" onfocus="focusID(this)" type="text"></td><td valign="top"><input name="ind1txt" size="1" maxlength="1" onfocus="focusID(this)" type="text"></td><td valign="top"><input name="ind2txt" size="1" maxlength="1" onfocus="focusID(this)" type="text"></td><td>' + subFieldInputHTML()+ '</td></tr>';
}

function subFieldInputHTML(){
	return '$<input name="subfieldtxt" size="1" maxlength="1" onfocus="focusID(this)" type="text"><input name="fieldvalue" size="26" onfocus="focusID(this)" type="text"><br/>';
}